package ContentManagers.RoleManagers;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Martin San Diego
 */
public class AuthenticationManager {
    
    //this class is designed to improve session management and reduce brute force checking
    
    public boolean inSuchDB(int fromDB, HttpServletRequest request, HttpServletResponse response)
    {   
        boolean retVal = false;
        HttpSession session = request.getSession();
                        
        if(fromDB == 1) //customer session db
            {
                retVal = new CustomerSessionManager().checkSession(session.getAttribute("username").toString(), request.getRemoteAddr().toString(), session.getAttribute("sessionID").toString());
            }
            else if (fromDB == 2) //admin session db
                {
                    retVal = new AdminSessionManager().checkSession(session.getAttribute("username").toString(), request.getRemoteAddr().toString(), session.getAttribute("sessionID").toString());
                }
                else if (fromDB == 3) //product session manager db
                    {
                        retVal = new PMSessionManager().checkSession(session.getAttribute("username").toString(), request.getRemoteAddr().toString(), session.getAttribute("sessionID").toString());
                    }
                    else if (fromDB == 4) //accounting man session db
                        {
                            retVal = new AMSessionManager().checkSession(session.getAttribute("username").toString(), request.getRemoteAddr().toString(), session.getAttribute("sessionID").toString());
                        }
        return retVal;
    }
    
    public void redirectToProperIndex(int fromContext, HttpServletRequest request, HttpServletResponse response)
    {
            HttpSession session = request.getSession();
            boolean checkDB = false;
            
            //1,2,3,4
            if(fromContext == 0)
            {
                checkAll(request, response, session);
            }
            else if(fromContext == 1) //dont check customer session db
            {
                checkExceptCustomer(request, response, session);
            }
            else if (fromContext == 2) //dont check admin session db
                {
                    checkExceptAdmin(request, response, session);
                }
                else if (fromContext == 3) //dont check product session manager db
                    {
                       checkExceptProduct(request, response, session);
                    }
                    else if (fromContext == 4) //dont check accounting man session db
                        {
                            checkExceptAccounting(request, response, session);
                        }
       
    }
    
    public void checkAll(HttpServletRequest request, HttpServletResponse response, HttpSession session)
    {
        try{
            boolean checkDB = false;
            
            checkDB = new CustomerSessionManager().checkSession(session.getAttribute("username").toString(), request.getRemoteAddr().toString(), session.getAttribute("sessionID").toString());
            if(checkDB == true)
                response.sendRedirect(request.getContextPath() + "/user_index.jsp");
            else{
                
                    checkDB = new AdminSessionManager().checkSession(session.getAttribute("username").toString(), request.getRemoteAddr().toString(), session.getAttribute("sessionID").toString());
                    if(checkDB == true)
                        response.sendRedirect(request.getContextPath() + "/Admin Interface/admin_index.jsp");
                    else 
                        {   
                            checkDB = new PMSessionManager().checkSession(session.getAttribute("username").toString(), request.getRemoteAddr().toString(), session.getAttribute("sessionID").toString());
                            if(checkDB == true)
                                response.sendRedirect(request.getContextPath() + "/Managing Interface/managing_index.jsp");
                            else
                                {
                                    checkDB = new AMSessionManager().checkSession(session.getAttribute("username").toString(), request.getRemoteAddr().toString(), session.getAttribute("sessionID").toString());
                                    if(checkDB == true)
                                        response.sendRedirect(request.getContextPath() + "/Managing Interface/managing_index.jsp");
                                    
                                }
                        }
            }
                    
            
        }catch(Exception e)
           {}
    }
    
    public void checkExceptCustomer(HttpServletRequest request, HttpServletResponse response, HttpSession session)
    {
           boolean checkDB = false;
           
           try{
           checkDB = new AdminSessionManager().checkSession(session.getAttribute("username").toString(), request.getRemoteAddr().toString(), session.getAttribute("sessionID").toString());
           
            if(checkDB == true)
                response.sendRedirect(request.getContextPath() + "/Admin Interface/admin_index.jsp");
            else 
                {   
                    checkDB = new PMSessionManager().checkSession(session.getAttribute("username").toString(), request.getRemoteAddr().toString(), session.getAttribute("sessionID").toString());
                    if(checkDB == true)
                        response.sendRedirect(request.getContextPath() + "/Managing Interface/managing_index.jsp");
                    else
                        {
                            checkDB = new AMSessionManager().checkSession(session.getAttribute("username").toString(), request.getRemoteAddr().toString(), session.getAttribute("sessionID").toString());
                            if(checkDB == true)
                                response.sendRedirect(request.getContextPath() + "/Managing Interface/managing_index.jsp");
                            else
                                response.sendRedirect(request.getContextPath() + "/index.jsp");
                                
                        }
                }
           }catch(Exception e)
           {}
    }
    
    public void checkExceptAdmin(HttpServletRequest request, HttpServletResponse response, HttpSession session)
    {
        boolean checkDB = false;
           
           try{
           checkDB = new CustomerSessionManager().checkSession(session.getAttribute("username").toString(), request.getRemoteAddr().toString(), session.getAttribute("sessionID").toString());
           
            if(checkDB == true)
                response.sendRedirect(request.getContextPath() + "/user_index.jsp");
            else 
                {   
                    checkDB = new PMSessionManager().checkSession(session.getAttribute("username").toString(), request.getRemoteAddr().toString(), session.getAttribute("sessionID").toString());
                    if(checkDB == true)
                        response.sendRedirect(request.getContextPath() + "/Managing Interface/managing_index.jsp");
                    else
                        {
                            checkDB = new AMSessionManager().checkSession(session.getAttribute("username").toString(), request.getRemoteAddr().toString(), session.getAttribute("sessionID").toString());
                            if(checkDB == true)
                                response.sendRedirect(request.getContextPath() + "/Managing Interface/managing_index.jsp");
                            else
                                response.sendRedirect(request.getContextPath() + "/index.jsp");
                                
                        }
                }
           }catch(Exception e)
           {}
        
    }
    
    public void checkExceptProduct(HttpServletRequest request, HttpServletResponse response, HttpSession session)
    {
        boolean checkDB = false;
           
           try{
           checkDB = new CustomerSessionManager().checkSession(session.getAttribute("username").toString(), request.getRemoteAddr().toString(), session.getAttribute("sessionID").toString());
           
            if(checkDB == true)
                response.sendRedirect(request.getContextPath() + "/user_index.jsp");
            else 
                {   
                    checkDB = new AdminSessionManager().checkSession(session.getAttribute("username").toString(), request.getRemoteAddr().toString(), session.getAttribute("sessionID").toString());
                    if(checkDB == true)
                        response.sendRedirect(request.getContextPath() + "/Admin Interface/admin_index.jsp");
                    else
                        {
                            checkDB = new AMSessionManager().checkSession(session.getAttribute("username").toString(), request.getRemoteAddr().toString(), session.getAttribute("sessionID").toString());
                            if(checkDB == true)
                                response.sendRedirect(request.getContextPath() + "/Managing Interface/managing_index.jsp");
                            else
                                response.sendRedirect(request.getContextPath() + "/index.jsp");
                                
                        }
                }
           }catch(Exception e)
           {}
        
    }
    
    public void checkExceptAccounting(HttpServletRequest request, HttpServletResponse response, HttpSession session)
    {
        boolean checkDB = false;
        
        try{
           checkDB = new CustomerSessionManager().checkSession(session.getAttribute("username").toString(), request.getRemoteAddr().toString(), session.getAttribute("sessionID").toString());
           
            if(checkDB == true)
                response.sendRedirect(request.getContextPath() + "/user_index.jsp");
            else 
                {   
                    checkDB = new AdminSessionManager().checkSession(session.getAttribute("username").toString(), request.getRemoteAddr().toString(), session.getAttribute("sessionID").toString());
                    if(checkDB == true)
                        response.sendRedirect(request.getContextPath() + "/Admin Interface/admin_index.jsp");
                    else
                        {
                            checkDB = new PMSessionManager().checkSession(session.getAttribute("username").toString(), request.getRemoteAddr().toString(), session.getAttribute("sessionID").toString());
                            if(checkDB == true)
                                response.sendRedirect(request.getContextPath() + "/Managing Interface/managing_index.jsp");
                            else
                                response.sendRedirect(request.getContextPath() + "/index.jsp");
                                
                        }
                }
           }catch(Exception e)
           {}
        
    }
            


}
   
